Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(serialport): add mutex for thread-safe access of isClosing bool status #1009

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dido18
Copy link
Contributor

@dido18 dido18 commented Jan 16, 2025

Please check if the PR fulfills these requirements

  • The PR has no duplicates (please search among the Pull Requests
    before creating one)
  • Tests for the changes have been added (for bug fixes / features)
  • What kind of change does this PR introduce?
    Fix a DATA RACE on the isClosing variable of the seiral port.
  • What is the current behavior?
    There are two routines that read and write the isClosing variable concurrently.
    • the routine of the open command reads the isClosing variable to verify if it must stop reading from the serial port
    • the routine of the close command writes the isClosing (setting to true) variable to inform that the serial port must be closed.
  • What is the new behavior?
    Add a mutex to control the access of the shared variable (another approach could have been adding a channel to inform to close the serial port)
  • Does this PR introduce a breaking change?
  • Other information:

@dido18 dido18 linked an issue Jan 16, 2025 that may be closed by this pull request
3 tasks
@codecov-commenter
Copy link

codecov-commenter commented Jan 16, 2025

Codecov Report

Attention: Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.

Project coverage is 20.09%. Comparing base (67db428) to head (33d9e31).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
serialport.go 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1009      +/-   ##
==========================================
- Coverage   20.14%   20.09%   -0.05%     
==========================================
  Files          42       42              
  Lines        3221     3229       +8     
==========================================
  Hits          649      649              
- Misses       2487     2495       +8     
  Partials       85       85              
Flag Coverage Δ
unit 20.09% <0.00%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dido18 dido18 requested a review from a team January 17, 2025 09:17
@dido18 dido18 changed the title fix(serialport): add mutex for thread-safe access in serialport isClosing fix(serialport): add mutex for thread-safe access of isClosing bool status Jan 17, 2025
@dido18 dido18 self-assigned this Jan 17, 2025
Copy link
Member

@cmaglie cmaglie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only need to synchronize access to a single variable maybe an atomic.Bool could be better?

Comment on lines +90 to +98
p.mu.Lock()
//if we detect that port is closing, break out of this for{} loop.
if p.isClosing {
strmsg := "Shutting down reader on " + p.portConf.Name
log.Println(strmsg)
h.broadcastSys <- []byte(strmsg)
break
}
p.mu.Unlock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the break path is taken doesn't the mutex remain locked?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Data Race on Close command
3 participants